﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;

namespace _1042009_1042034
{
    public partial class uctThemDanhMucPhong : UserControl
    {
        public uctThemDanhMucPhong()
        {
            InitializeComponent();
        }

        /////////////
                
        int iColStt;
        int iColMaPhong;
        int iColDonGia;
        int iColTenPhong;
        int iColMaLoaiPhong;
        int iColMaTinhTrang;
        int iColGhiChu;
        int iColCheck;
        protected void CreateColumn()
        {
            this.dgv.Columns.Clear();
            List<LoaiPhongDTO> ListLoaiPhong = Business.LoaiPhongService.LayDanhSach();
            List<TinhTrangDTO> ListTinhTrang = Business.TinhTrangService.LayDanhSach();
            DataTable DataLoaiPhong = Business.LoaiPhongService.ConvertListLoaiPhongDTOToDataTable(ListLoaiPhong);
            DataTable DataTinhTrang = Business.TinhTrangService.ConvertListTinhTrangDTOToDataTable(ListTinhTrang);
            
            this.dgv.AddTextColumn(true, "STT", "STT");
            this.dgv.AddTextColumn(true,"MaPhong", "Mã Phòng");
            this.dgv.AddTextColumn(false, "TenPhong", "Tên Phòng");
            this.dgv.AddComboBoxColumns(false, "MaLoaiPhong", "Loại Phòng", DataLoaiPhong, LoaiPhongDTO.ValueMember, LoaiPhongDTO.DisplayMember);
            this.dgv.AddComboBoxColumns(true, "DonGia", "Đơn Giá", DataLoaiPhong, LoaiPhongDTO.ValueMember, "DonGia");
            this.dgv.AddTextColumn(false, "GhiChu", "Ghi Chú");
            this.dgv.AddComboBoxColumns(false, "MaTinhTrang", "Tình Trạng", DataTinhTrang, TinhTrangDTO.ValueMember, TinhTrangDTO.DisplayMember);
            this.dgv.AddCheckBoxColumn("Check", "Check");

            iColMaPhong = this.dgv.GetColumIndexByName("MaPhong");
            iColTenPhong = this.dgv.GetColumIndexByName("TenPhong");
            iColMaLoaiPhong = this.dgv.GetColumIndexByName("MaLoaiPhong");
            iColMaTinhTrang = this.dgv.GetColumIndexByName("MaTinhTrang");
            iColGhiChu = this.dgv.GetColumIndexByName("GhiChu");
            iColCheck = this.dgv.GetColumIndexByName("Check");
            iColDonGia = this.dgv.GetColumIndexByName("DonGia");
        }                
        void SetUpForm()
        {
            this.AutoSize = false;
            this.AutoScroll = false;
            this.Size = Form1.SizeContext;
            panel.Size = this.dgv.Size;
            panel.Controls.Add(this.dgv);
            panel.AutoScroll = false;
            panel.AutoSize = false;
        }
        protected void SetUpDataGridView()
        {
            this.dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            this.dgv.Rows.Clear();
            this.dgv.Columns.Clear();
            this.dgv.Location = new Point(0, 0);
            this.dgv.Size = new Size(Form1.SizeContext.Width - 80, 215);
            this.dgv.ScrollBars = ScrollBars.Both;
            this.dgv.TopLeftHeaderCell.Value = "Select All";
            this.dgv.BackgroundColor = Color.White;
            // dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader; 
           // cho hien dong trang.
            this.dgv.AllowUserToAddRows = true; 
        }

        protected void ReLoad()
        {
            this.dgv.m_Reload = true;
            this.dgv.AddRows();           
        }

        private void uctThemDanhMucPhong_Load(object sender, EventArgs e)
        {
            this.SetUpDataGridView();
            this.SetUpForm();
            this.CreateColumn();
            this.ReLoad();
            //su kien
            this.dgv.CellValueChanged += new DataGridViewCellEventHandler(dgv_CellValueChanged);
        }
        
        int iMaPhongIdentity = Business.PhongService.GetIdentity();
        int iStt = 0;

        void SetStt()
        {
            int Stt = (this.dgv.iPage - 1) * this.dgv.iRecord + 1;
            for (int i = 0; i < this.dgv.Rows.Count; i++)
            {
                this.dgv[this.iColStt, i].Value = Stt.ToString();
                this.dgv[this.iColMaPhong, i].Value = (iMaPhongIdentity + Stt).ToString();
                Stt++;
            }
        }

        void RemoveRow(int iRow)
        {
            iStt--;
            this.dgv.Rows.RemoveAt(iRow);
            this.SetStt();
        }

        void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int ColIndex = e.ColumnIndex;
            int RowIndex = e.RowIndex;
            
            if (ColIndex == this.iColMaLoaiPhong)
            {
                this.dgv[this.iColDonGia, RowIndex].Value = this.dgv[ColIndex, RowIndex].Value;
            }
            if (ColIndex != this.iColCheck && ColIndex != this.iColStt && ColIndex != this.iColMaPhong)
            {
                this.dgv.Rows[RowIndex].Cells[this.iColCheck].Value = "Checked";
                this.dgv.SetRowColor(RowIndex, MyColor.Change,true);
            }
        }       

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int CheckColIndex = this.iColCheck;
            for (int i = this.dgv.Rows.Count - 1; i >= 0 ; i--)
            {
                if (this.dgv.Rows[i].Cells[CheckColIndex].Value != null)
                {
                    if (this.dgv.Rows[i].Cells[CheckColIndex].Value.ToString() == "Checked")
                    {
                        //tien hanh xoa du lieu tren form
                        this.dgv.Rows[0].Cells[this.iColCheck].Selected = true;
                        this.RemoveRow(i);
                    }
                }
            }
        }

        PhongDTO GetDataOfRows(int iRow)
        {
            PhongDTO p = new PhongDTO();
            //neu du lieu trong fom khac null thi gan cho doi tuong.

            p.MaPhong = int.Parse(this.dgv.Rows[iRow].Cells[iColMaPhong].Value.ToString());
            if (this.dgv.Rows[iRow].Cells[iColMaLoaiPhong].Value != null)
            {
                p.MaLoaiPhong = int.Parse(this.dgv.Rows[iRow].Cells[iColMaLoaiPhong].Value.ToString());
            }
            if (this.dgv.Rows[iRow].Cells[iColMaTinhTrang].Value != null)
            {
                p.MaTinhTrang = int.Parse(this.dgv.Rows[iRow].Cells[iColMaTinhTrang].Value.ToString());
            }
            if (this.dgv.Rows[iRow].Cells[iColTenPhong].Value != null)
            {
                p.TenPhong = (this.dgv.Rows[iRow].Cells[iColTenPhong].Value.ToString());
            }
            if (this.dgv.Rows[iRow].Cells[iColGhiChu].Value != null)
            {
                p.GhiChu = (this.dgv.Rows[iRow].Cells[iColGhiChu].Value.ToString());
            }

            return p;
        }
        List<PhongDTO> ListPhong = new List<PhongDTO>();
        private void btnThem_Click(object sender, EventArgs e)
        { 
            int CheckColIndex = this.iColCheck;
            PhongDTO objPhong = new PhongDTO();
            int Dem = 0;
            for (int i = 0; i < this.dgv.Rows.Count; i++)
            {
                if (this.dgv.Rows[i].Cells[CheckColIndex].Value != null)
                {
                    if (this.dgv.Rows[i].Cells[CheckColIndex].Value.ToString() == "Checked")
                    {
                        Dem++;
                        objPhong = this.GetDataOfRows(i);
                        PhongDTO p = Business.PhongService.LayPhongTheoTen(objPhong.TenPhong);
                        if (objPhong.KiemTra() == false || p != null)
                        {
                            this.dgv.SetRowColor(i, MyColor.Eror, true);
                        }
                        else//hop le
                        {
                            ListPhong.Add(objPhong);
                            Dem--;
                        }
                    }
                }
            }
            if (Dem == 0)
            {
                for (int i = 0; i < this.ListPhong.Count; i++)
                {
                    Business.PhongService.Them(ListPhong[i]);
                }
                this.iMaPhongIdentity = Business.PhongService.GetIdentity();
                MessageBox.Show("Dữ liệu đã được thêm");
            }
            ListPhong.Clear();
        }

        private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int iRow = this.dgv.SelectedCells[0].RowIndex;
                Color x = dgv[this.iColCheck, iRow].Style.BackColor;
                dgv.SetRowColor(iRow, x);
            }
            catch { }
            
        }

        private void dgv_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int iRow = this.dgv.SelectedCells[0].RowIndex;
                Color x = dgv[this.iColCheck, iRow].Style.BackColor;
                dgv.SetRowColor(iRow, x, true);
            }
            catch { }
        }

        private void dgv_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int iRow = e.RowIndex;
                DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)this.dgv[this.iColCheck, iRow];
                if (checkcell.Value.ToString() == "Checked")
                {
                    checkcell.Value = "Unchecked";
                }
                else
                {
                    checkcell.Value = "Checked";
                }
            }
            catch { }
        }

        private void dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            this.SetStt();
        }               
    }
}
